package com.example.administrator.hqq.fragment.discover;


import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.hqq.R;
import com.example.administrator.hqq.adapter.FragmentsAdapter;
import com.example.administrator.hqq.base.MyFragemnt;
import com.example.administrator.hqq.bean.MainActivityEventData;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * 贴子与群聊
 */
public class ConventionDlalogFragment extends MyFragemnt {

    Unbinder unbinder;
    @BindView(R.id.return_auto)
    AutoLinearLayout returnAuto;
    @BindView(R.id.post)
    AutoRelativeLayout post;
    @BindView(R.id.post_view)
    View postView;
    @BindView(R.id.post_auto)
    AutoRelativeLayout postAuto;
    @BindView(R.id.group)
    AutoRelativeLayout group;
    @BindView(R.id.group_view)
    View groupView;
    @BindView(R.id.group_auto)
    AutoRelativeLayout groupAuto;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.screen_man_tv)
    TextView screenManTv;
    @BindView(R.id.screen_man)
    AutoLinearLayout screenMan;
    @BindView(R.id.screen_woman_tv)
    TextView screenWomanTv;
    @BindView(R.id.screen_woman)
    AutoLinearLayout screenWoman;
    @BindView(R.id.screen_age_tv)
    TextView screenAgeTv;
    @BindView(R.id.screen_age)
    AutoLinearLayout screenAge;
    @BindView(R.id.screen_confirm)
    Button screenConfirm;
    @BindView(R.id.nav_view)
    NavigationView navView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @BindView(R.id.convention_module_guide)
    ImageView conventionModuleGuide;
    private boolean isShow = true;
    private String TAG = this.getClass().getSimpleName();
    private View inflate;
    private List<Fragment> fragments;
    private String s;
    private GroupChatListFragment groupChatListFragment;
    private PostModuleFragment postModuleFragment;

    public ConventionDlalogFragment() {
        // Required empty public constructor
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_convention_dlalog, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        initview();
        return inflate;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("ResourceAsColor")
    private void initview() {
        screenManTv.setTextColor(Color.parseColor("#000000"));
        screenWomanTv.setTextColor(Color.parseColor("#000000"));
        screenWoman.setSelected(true);
        screenMan.setSelected(true);
        postAuto.setSelected(true);
        fragments = new ArrayList<>();
        groupChatListFragment = new GroupChatListFragment();
        postModuleFragment = new PostModuleFragment();
        fragments.add(postModuleFragment);
        fragments.add(groupChatListFragment);
        FragmentsAdapter fragmentsAdapter = new FragmentsAdapter(getChildFragmentManager(), fragments);
        viewpager.setAdapter(fragmentsAdapter);
        viewpager.setCurrentItem(0);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    postAuto.setSelected(true);
                    groupAuto.setSelected(false);
                } else {
                    groupAuto.setSelected(true);
                    postAuto.setSelected(false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(String s) {

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("ResourceType")
    @OnClick({R.id.return_auto, R.id.post_auto, R.id.group_auto, R.id.screen_man, R.id.screen_woman, R.id.screen_age, R.id.screen_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.return_auto:
                EventBus.getDefault().post(new MainActivityEventData(null, 0));
                break;
            case R.id.post_auto:
                if (!postAuto.isSelected()) {
                    postAuto.setSelected(!postAuto.isSelected());
                    groupAuto.setSelected(!groupAuto.isSelected());
                    viewpager.setCurrentItem(0);
                }
                break;
            case R.id.group_auto:
                if (!groupAuto.isSelected()) {
                    postAuto.setSelected(!postAuto.isSelected());
                    groupAuto.setSelected(!groupAuto.isSelected());
                    viewpager.setCurrentItem(1);
                }
                break;
//            case R.id.screen:
//                drawerLayout.openDrawer(Gravity.RIGHT);
//                break;
            case R.id.screen_man:
                setScreenTextColor(screenMan, screenManTv);
                break;
            case R.id.screen_woman:
                setScreenTextColor(screenWoman, screenWomanTv);
                break;
//            case R.id.screen_area:
//                setScreenTextColor(screenArea, screenAreaTv);
//                break;
            case R.id.screen_age:
                setScreenTextColor(screenAge, screenAgeTv);
                break;
            case R.id.screen_confirm:
                if (screenMan.isSelected() && screenWoman.isSelected()) {
                    groupChatListFragment.setGender_prefer("0");
                    postModuleFragment.setGender_prefer("0");
                } else if (screenMan.isSelected() && !screenWoman.isSelected()) {
                    groupChatListFragment.setGender_prefer("1");
                    postModuleFragment.setGender_prefer("1");
                } else if (screenWoman.isSelected() && !screenMan.isSelected()) {
                    groupChatListFragment.setGender_prefer("2");
                    postModuleFragment.setGender_prefer("2");
                } else {
                    groupChatListFragment.setGender_prefer("0");
                    postModuleFragment.setGender_prefer("0");
                }
                String age = screenAge.isSelected() ? "1" : "0";
                groupChatListFragment.setAge_prefer(age);
                postModuleFragment.setAge_prefer(age);
                //将同城筛选项去掉
//                String area = screenArea.isSelected() ? "1" : "0";
                String area = "0";
                groupChatListFragment.setCity_prefer(area);
                postModuleFragment.setCity_prefer(area);
                groupChatListFragment.startInitData();
                postModuleFragment.startInitData();
                Log.e(TAG, "onViewClicked: " + age + "++++++" + area);
                drawerLayout.closeDrawer(Gravity.RIGHT);
                break;
        }
    }

    public void setTopic_id(String topic_id) {
        groupChatListFragment.setTopic_id(topic_id);
        postModuleFragment.setTopic_id(topic_id);
        groupChatListFragment.startInitData();
        postModuleFragment.startInitData();
    }

    public void setTopicName(String topicName) {
        groupChatListFragment.setTopciName(topicName);
        postModuleFragment.setTopicName(topicName);
    }

    @SuppressLint("ResourceType")
    public void setScreenTextColor(AutoLinearLayout auto, TextView screenAgeTv) {
        auto.setSelected(!auto.isSelected());
        s = auto.isSelected() ? "#000000" : "#cccac7";
        screenAgeTv.setTextColor(Color.parseColor(s));
    }

    @OnClick(R.id.convention_module_guide)
    public void onViewClicked() {
    }
}
